using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

// FIXME: We are using our own UnixClient and UnixListener
// from Dashboard.Util until the patch for 2.0 has been
// accepted upstream.
//using Mono.Unix;

using Dashboard.Util;

namespace Dashboard
{
	public class UnixConnection : Connection
	{
		private UnixClient client;
		private NetworkStream stream;
		
		public UnixConnection ()
		{
			string socket_path = Path.Combine (PathFinder.UserData, "dashboard");		      

			client = new UnixClient ();
			client.Connect (new Mono.Unix.UnixEndPoint (socket_path));

			stream = client.GetStream ();
		}
		
		public override void Send (Message message)
		{
			Message.Serializer.Serialize (stream, message);

			// 0xff indicates end-of-message
			stream.WriteByte (0xff);
		}

		public override void Subscribe (string pattern, MessageHandler handler)
		{
			throw new NotImplementedException ();
		}

		public override void Dispose ()
		{
			stream.Close ();

			client.Close ();
			client = null;
		}

		private class UnixConnectionHandler : XmlConnectionHandler
		{
			private UnixConnectionHandler (UnixClient client)
			{

			}

			protected override Stream Stream
			{
				get { return null; }
			}

			protected override bool Closed
			{
				get { return false; }
			}

			public override void Close ()
			{

			}
		}
	}
}
